Import('env')
import os, SCons
if env['RELAY']:
	from muscons import Relay

sources_libmuscan = Split("""
    scandir.cc
    mp3.c
    scanner.cc
""")


env_libmuscan = env.Copy()
conf = env_libmuscan.Configure()

if env['VORBIS']:
	env_libmuscan.ParseConfig('pkg-config --libs --cflags vorbisfile')
	if conf.CheckLibWithHeader('', 'vorbis/vorbisfile.h', 'C++', 'ov_clear(0);'):
		#print SCons.__version__
		sconsversion = SCons.__version__
		sv = sconsversion.split(".")
		try:
			if len(sv) > 2:
				major, minor, micro = sv[0], sv[1], sv[2]
			else:
				major, minor, micro = sv[0], sv[1], 0
			if int(minor) == 96 and int(micro) >= 93:
				env_libmuscan.Append(CPPDEFINES = 'HAVE_VORBIS=1')
			else:
				env_libmuscan.Append(CPPDEFINES = {'HAVE_VORBIS':1})
		except Exception, error:
			print "ERROR:", error
		print "OGG Vorbis found, compiling into muscan."
	else:
		print "OGG Vorbis NOT found, not compiled into muscan."
else:
	print "OGG Vorbis manually disabled, not compiled into muscan."

conf.Finish()

if env['RELAY']:
	stubs = Relay(env_libmuscan, 'vorbisfile', 'vorbis', 'ogg')
else:
	stubs = []

env_libmuscan.StaticLibrary(target = 'muscan', source = sources_libmuscan + stubs)

if env['LIBXMLPP_STATIC']:
	xsources = env['LIBXMLPP_STATIC_LIBS']
else:
	xsources = []

env_muscan = env_libmuscan.Copy()
env_muscan.Append(LIBS = ['muscan', 'muhelp'])
muscan = env_muscan.Program(target = 'muscan', source = ['muscan.cc'] + xsources)

install = env_muscan.Install(env['DESTDIR'] + env['BINDIR'], [muscan])
manfile = ["muscan.1"]
man_install = env.Install(env['DESTDIR'] + env['MANDIR'], manfile)
env.Alias('install_muscan', man_install)
env.Alias('install_muscan', install)
env.Alias('install', 'install_muscan')
print "* muscan will be installed"

env_muscand = env_libmuscan.Copy()
env_muscand.SConsignFile()
conf = env_muscand.Configure()

if conf.CheckLibWithHeader('fam', 'fam.h', 'C++', 'FAMOpen2(0,0);'):
	env_muscand.Append(LIBS = ['muscan', 'muhelp'])
	if env['RELAY']:
		stubs = Relay(env_muscand, 'fam')
	else:
		stubs = []
	muscand = env_muscand.Program(target = "muscand", source = ['muscand.cc'] + stubs + xsources)
	manfile =  [ "muscand.1"]
	man_install = env.Install(env['DESTDIR'] + env['MANDIR'], manfile)
	install = env_muscand.Install(env['DESTDIR'] + env['BINDIR'], [muscand])
	env.Alias('install_muscand', man_install)
	env.Alias('install_muscand', install)
	env.Alias('install', 'install_muscand')
	print "* muscand will be installed"
	


conf.Finish()
print